<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>

      <template #toolbar>
        <div>
          <a-button
            class="m-1 pr-1"
            v-for="(item, index) in butData"
            :key="index"
            :type="item.type"
            @click="item.do"
            v-auth="item.auth"
          >
            <Icon :icon="item.icon" /> {{ t(item.title) }}
          </a-button>
        </div>
      </template>
      <!-- 表格插槽 -->
      <template #djno="{ record }">
        <a @click="handleForm({ djno: record.djno, bred: record.bred })">
          {{ record.djno }}
        </a>
      </template>
    </BasicTable>

    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8OrderFlowWxM8OrderFlowWxInList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    listSet,
    customListData,
    m8OrderFlowWxInDelete,
    m8OrderFlowWxInSp,
    m8OrderFlowWxInDeSp,
  } from '/@/api/m8/order/m8OrderFlowWxIn';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { handleExport } from '/@/utils/export';

  const { t } = useI18n('order.flow.wx.m8OrderFlowWxIn');
  const { showMessage } = useMessage();

  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度
    schemas: [],
  });

  const props = defineProps({
    bred: { type: String, default: '0' },
  });

  let tableColumns = ref<any>([]);
  let butData = ref<any>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });
  let title = ref('');

  const actionColumn: BasicColumn = {
    width: 100, //操作按钮宽度
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑'),
        onClick: handleForm.bind(this, { djno: record.djno }),
        auth: 'order:flow:wx:m8OrderFlowWxIn:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除'),
        popConfirm: {
          title: t('是否确认删除发料单'),
          confirm: handleDelete.bind(this, { id: record.djno, outStatus: record.outStatus }),
        },
        auth: 'order:flow:wx:m8OrderFlowWxIn:edit',
        ifShow: record.inStatus != 4,
      },
    ],
  };

  onMounted(async () => {
    const res = await listSet({ viewCode: 'M8_flow_wx_in_list' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);

    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头

    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.btnKey == 'btnSp') {
        item.do = () => handleSp();
      }
      if (item.btnKey == 'btnBack') {
        item.do = () => handleDeSp();
      }
      if (item.btnKey == 'btnExport') {
        item.do = () => btnExport();
      }
    });

    // actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      api: customListData,
      beforeFetch: (params) => {
        params.ddate_gte = params.ddate ? params.ddate[0] : '';
        params.ddate_lte = params.ddate ? params.ddate[1] : '';
        params.ddate = '';
        params.url = listTabConfig.value.api;
        params.ddate_gte = params.ddate ? params.ddate[0] : '';
        params.ddate_lte = params.ddate ? params.ddate[1] : '';
        params.ddate = '';
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
    });
    if (!listTabConfig.value.immediate) {
      reload();
    }
  });

  const [registerDrawer, { openDrawer: openDrawer }] = useDrawer();

  const [registerTable, { reload, setProps, getSelectRows, getForm }] = useTable({
    actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
    rowSelection: {
      type: 'checkbox',
    },
  });
  // 刷新表格数据（含表单回调）
  function handleSuccess() {
    reload();
  }
  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  //审核
  async function handleSp() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await m8OrderFlowWxInSp({ selIds: selIds.value, ope: 4 });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 反审
  async function handleDeSp() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await m8OrderFlowWxInDeSp({ selIds: selIds.value, ope: 1 });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  async function handleDelete(record: Recordable) {
    const res = await m8OrderFlowWxInDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  //导出数据
  function btnExport() {
    var formdata = getForm().getFieldsValue();
    formdata = { ...formdata, ...props };
    handleExport('/m8/order/flow/wx/m8OrderFlowWxIn/exportData', formdata);
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  /* .ant-select-show-search {
    width: 100% !important ;
  } */
  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
